package com.yoomiito.app.ui.order.detail;

import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseActPresenter;
import com.yoomiito.app.model.order.OrderDetailInfo;
import com.yoomiito.app.model.order.PayResultInfo;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.pay.PayTypeUtils;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.NetworkUtils;
import com.yoomiito.app.utils.YMToast;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.BaseModel;
import cn.droidlover.xdroidmvp.net.XApi;

public class OrderInfoPresenter extends BaseActPresenter<OrderInfoActivity> {
    public OrderInfoPresenter(AppConfig appConfig) {
        super(appConfig);
    }

    public void getOrderInfo(String orderId, int type) {
        if (NetworkUtils.checkNetPermission(getV())) {
            return;
        }
        YmApi.getYmService()
                .getOrderInfo(orderId)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<OrderDetailInfo>(handleException()) {
                    @Override
                    public void onSuccess(OrderDetailInfo info) {
                        if (hasV())
                            getV().setOrderInfo(info);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        if (hasV())
                            getV().getDataFail();
                    }
                });
    }


    public void confirmOrder(String orderId, int goodsType) {
        if (NetworkUtils.checkNetPermission(getV())) {
            return;
        }
        YmApi.getYmService()
                .confirmOrder(orderId)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>() {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        if (hasV())
                            getV().confirmOrder(true);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        if (hasV())
                            getV().confirmOrder(false);
                    }
                });
    }

    public void cancelOrder(int order_type, String order_id) {
        YmApi.getYmService()
                .cancelOrder(order_id)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>(handleException()) {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        if (hasV())
                            getV().updateOrderInfo();
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

    public void pay(String id, int wx, int ali, String account, int orderType, String psw, String yoMoneyPay) {
        YmApi.getYmService()
                .multipPay(id, wx, ali, account, orderType, psw, yoMoneyPay)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<PayResultInfo>() {
                    @Override
                    public void onSuccess(PayResultInfo payResultInfo) {
                        if (getV() == null) return;
                        getV().dismissLoading();
                        getV().mQueryOrderID = payResultInfo.getOut_trade_no();
                        getV().pay_type = payResultInfo.getPay_type();
                        if ("wxpay".equals(payResultInfo.getPay_type())) {
                            PayTypeUtils.wxPay(payResultInfo.getData());
                        } else {
                            PayTypeUtils.aliPay(getV(), payResultInfo.getData().getAlipay_order());
                        }
                    }

                    @Override
                    public void onFail(ApiException error) {
                        if (getV() == null) return;
                        YMToast.showText(error.getMsg());
                        getV().dismissLoading();
                    }
                });
    }

    public void queryOrder(String orderId, String pay_type) {
        if (NetworkUtils.checkNetPermission(getV())) {
            return;
        }
        EventMessage eventMessage = new EventMessage();
        YmApi.getYmService()
                .quseryOrderStatus(orderId, pay_type)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>(handleException()) {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        if (getV() == null) return;
                        eventMessage.setCode(AllCode.Success);
                        getV().payResult(eventMessage);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        if (hasV()) {
                            eventMessage.setCode(AllCode.Fail);
                            getV().payResult(eventMessage);
                        }
                    }
                });
    }

}
